package com.sq.partner.manager.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.ITradNoCarProdConInsureDao;
import com.sq.partner.manager.mybatis.model.NocarConInsu;
import com.sq.partner.manager.util.DBUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;

@Repository
public class ITradNoCarProdConInsureDaoImpl extends BaseDaoImpl implements ITradNoCarProdConInsureDao {
	private static Logger logger = LogUtil.getLog(ITradNoCarProdConInsureDaoImpl.class);
	@Override
	public Page<NocarConInsu> queryProdConInsureInfoPage(Page<NocarConInsu> page, String prodCode) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			conn = this.getConnection();
			String sql = "SELECT T.PROD_CODE as prodCode,T.SEQ_NO as seqNo,T.CON_CONF_NO as conConfNo,T.CON_DEPT_CODE as conDeptCode,T1.CODE_NAME as insuCompanyName,T.CON_PROP*100 as conProp,T.MAIN_CON_FLAG as mainConFlag,"
					+ " T.INP_DEPT_FLAG as inpDeptFlag,DECODE(T.MAIN_CON_FLAG, '0', '否', '1', '是', '') AS mainConFlagDscp,"
					+ " DECODE(T.INP_DEPT_FLAG, '0', '否', '1', '是', '') AS inpDeptFlagDscp"
					+ " FROM T_NOCAR_CON_INSU T INNER JOIN T_GATEWAY_DICT T1 "
					+ " ON T.CON_DEPT_CODE = T1.CODE AND T1.CODE_TYPE = 'insure_company' "
					+ " INNER JOIN T_NOCAR_PROD T2 ON T.PROD_CODE = T2.PROD_CODE "
					+ " WHERE T.PROD_CODE = '"+prodCode+"' ORDER BY T.SEQ_NO ASC";
			logger.debug("执行SQL：" + sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			List lists = DBUtil.transRs2List(rs);
			page.setData(lists);
		}finally{
			close(rs);
			close(pstmt);
			close(conn);
		}
		return page;
	}

	@Override
	public boolean saveProdConInsure(NocarConInsu noCarConInsu) throws Exception {
		return this.add("insertNocarConInsu", noCarConInsu);
	}

	@Override
	public boolean deleteProdConInsureByProdCode(String prodCode) throws Exception {
		return this.delete("delNocarConInsuByProdCode", prodCode);
	}

	@Override
	public boolean searchProdConInsureInfoByProdCode(String prodCode) throws Exception {
		Integer isLink = (Integer) this.query("isExistProdConInsuCountByProdCode", prodCode); 
		return isLink.intValue()>1;
	}
}
